import axios from 'axios'

const baseURL = import.meta.env.VITE_API_PATH

const server = axios.create({
  baseURL, // api的base_url
  timeout: 35000 // 请求超时时间
})

// 请求拦截
server.interceptors.request.use(
  (request) => {
    return request
  },
  (error) => Promise.reject(error)
)

// 响应拦截
server.interceptors.response.use(
  (response) => {
    // 如果是流直接返回
    if (response.data instanceof Blob) {
      return response
    }
    // 请求的响应体内包含code状态值
    return [response.data, response]
  },
  (err) => {
    let response = err.response
    return response
  }
)

export default server
